<?php
// +----------------------------------------------------------------------
// | author: thinkphp
// +----------------------------------------------------------------------

namespace app\api\lists\line;

use app\api\lists\BaseApiDataLists;
use app\common\enum\YesNoEnum;
use app\common\lists\ListsSearchInterface;
use app\common\model\travel\LinePrice;

/**
 * 列表
 * Class LinePriceLists
 * @package app\api\lists\article
 */
class LinePriceLists extends BaseApiDataLists implements ListsSearchInterface
{

    /**
     * @notes 搜索条件
     * @return \string[][]
     */
    public function setSearch(): array
    {
        return [
            '=' => ['line_id']
        ];
    }


    /**
     * @notes 自定查询条件
     * @return array
     */
    public function queryWhere()
    {
        $where[] = ['status', '=', YesNoEnum::YES];

        if (!empty($this->params['line_id'])) {
            $where[] = ['line_id', '=', $this->params['line_id']];
        }
        return $where;
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function lists(): array
    {
//        $where[] = ['status', '=', YesNoEnum::YES];
        $orderRaw = 'travel_time asc';
        $lists = LinePrice::where('travel_time', '>=', strtotime("+2 days", time()))
            ->where($this->queryWhere())
            ->where($this->searchWhere)
            ->orderRaw($orderRaw) // 优先展示最新价格
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();
        foreach ($lists as &$item) {
            $item['travel_time'] = date('Y-m-d', $item['travel_time']);
        }
        return $lists;
    }


    /**
     * @notes 获取数量
     * @return int
     */
    public function count(): int
    {
        return LinePrice::alias('l')
            ->where($this->queryWhere())
            ->count();
    }
}